package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.EngineerTypeResult;

import java.util.List;

/**
 * Created by mlli on 2017/11/6.
 */

public class EngTypeAdapter extends RecyclerView.Adapter{
    private Context context;
    private List<EngineerTypeResult.EngineerType> list;
    private ResultListener resultListener;

    public interface ResultListener{
        void setMsg(String msg);
    }

    public void setResultListener(ResultListener resultListener) {
        this.resultListener = resultListener;
    }

    public EngTypeAdapter(Context context, List<EngineerTypeResult.EngineerType> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<EngineerTypeResult.EngineerType> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.left_textview, null);
        return new MHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MHolder mHolder = (MHolder) holder;
        EngineerTypeResult.EngineerType type = list.get(position);
        final String name = type.getTypeName();
        mHolder.text.setText(name);
        mHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (resultListener!=null)resultListener.setMsg(name);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }
    class MHolder extends RecyclerView.ViewHolder{

        private final TextView text;

        public MHolder(View itemView) {
            super(itemView);
            text = (TextView) itemView.findViewById(R.id.title);
        }
    }
}
